package org.jeecg.modules.comment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.comment.entity.PhilComment;
import org.jeecg.modules.comment.service.IPhilCommentService;
import org.jeecg.modules.comment.vo.CommentPublish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 评论
 * @Author: jeecg-boot
 * @Date:   2020-11-12
 * @Version: V1.0
 */
@Api(tags="文章评论")
@RestController
@RequestMapping("/philComment")
@Slf4j
public class PhilCommentController extends JeecgController<PhilComment, IPhilCommentService> {
	@Autowired
	private IPhilCommentService philCommentService;
	/**
	 * TODO: 2020/11/12  分页列表查询
	 *
	 * @param philComment
	 * @param pageNo
	 * @param pageSize
	 * @param req  这里边的status控制查找什么类型的评论  0 未删除 1 删除 2发布
	 * @return
	 */
	@AutoLog(value = "pc评论-分页列表查询")
	@ApiOperation(value="pc评论-分页列表查询", notes="pc评论-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PhilComment philComment,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PhilComment> queryWrapper = QueryGenerator.initQueryWrapper(philComment, req.getParameterMap());
		// 控制查找什么类型的数据
		queryWrapper.eq("status", req.getParameter("status")==null?0:req.getParameter("status"));
		queryWrapper.eq("type",1);
		Page<PhilComment> page = new Page<>(pageNo, pageSize);
		IPage<PhilComment> pageList = philCommentService.page(page, queryWrapper);
		return Result.ok(pageList);
	}


	 @AutoLog(value = "App个人全部评论-分页列表查询")
	 @ApiOperation(value="App个人全部评论-分页列表查询", notes="App个人全部评论-分页列表查询")
	 @GetMapping(value = "/queryByOpenId")
	 public Result<?> queryPageByOpenId(@RequestParam("openId") String openId,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 Page<CommentPublish> page = new Page<>(pageNo, pageSize);
		 IPage<CommentPublish> pageList = philCommentService.queryListPageById(page, openId);
		 return Result.ok(pageList);
	 }

	 @AutoLog(value = "App文章发布的评论-分页列表查询")
	 @ApiOperation(value="App文章发布的评论-分页列表查询", notes="App文章发布的评论-分页列表查询")
	 @GetMapping(value = "/queryByArticleId")
	 public Result<?> queryByArticleId(@RequestParam("articleId") String articleId,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										HttpServletRequest req) {
		 Page<CommentPublish> page = new Page<>(pageNo, pageSize);
		 IPage<CommentPublish> pageList = philCommentService.queryListPublishPage(page, articleId);
		 return Result.ok(pageList);
	 }

	 /**
	 *   添加
	 *
	 * @param philComment
	 * @return
	 */
	@AutoLog(value = "评论-添加")
	@ApiOperation(value="评论-添加", notes="评论-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PhilComment philComment) {
		if (philComment.getCommentContent().isEmpty()||philComment.getCommentContent()==null||"".equals(philComment.getCommentContent())){
			return Result.error("评论不能为空！");
		}
		philComment.setType(1);
		philCommentService.save(philComment);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param philComment
	 * @return
	 */
	@AutoLog(value = "评论-编辑")
	@ApiOperation(value="评论-编辑", notes="评论-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PhilComment philComment) {
		philCommentService.updateById(philComment);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "评论-通过id删除")
	@ApiOperation(value="评论-通过id删除", notes="评论-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		philCommentService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "评论-批量删除")
	@ApiOperation(value="评论-批量删除", notes="评论-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.philCommentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "评论-通过id查询")
	@ApiOperation(value="评论-通过id查询", notes="评论-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PhilComment philComment = philCommentService.getById(id);
		if(philComment==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(philComment);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param philComment
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PhilComment philComment) {
        return super.exportXls(request, philComment, PhilComment.class, "评论");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PhilComment.class);
    }

}
